﻿using System;
using System.Collections.Generic;
using System.Xml;
using Def.Log;

namespace FrontDisplay.Weather
{
    public class WeatherInfo : WeatherItem
    {
        public string City { get; private set; }

        private readonly List<WeatherItem> _forecast = new List<WeatherItem>();

        public IList<WeatherItem> Forecast
        {
            get { return _forecast; }
        }

        public WeatherInfo(string xmlData)
        {
            try
            {
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(xmlData);
                if (xml.DocumentElement == null) return;

                string dateString = string.Empty;

                foreach (XmlNode node in xml.DocumentElement.ChildNodes[0].ChildNodes)
                {
                    if (node.Name == "yweather:location")
                    {
                        City = node.GetAttribute("city", string.Empty);
                        string country = node.GetAttribute("country", string.Empty);
                        if (!string.IsNullOrEmpty(country))
                        {
                            City += ", " + country;
                        }
                    }
                    if (node.Name == "item")
                    {
                        foreach (XmlNode xmlNode in node.ChildNodes)
                        {
                            if (xmlNode.Name == "yweather:condition")
                            {
                                Temperature = Convert.ToInt32(xmlNode.Attributes["temp"].Value);
                                //TODO: parse date string into Date property.
                                dateString = xmlNode.Attributes["date"].Value;
                                Text = xmlNode.Attributes["text"].Value;
                                Code = GetCode(xmlNode);
                            }
                            if (xmlNode.Name == "yweather:forecast")
                            {
                                DateTime date = DateTime.Parse(xmlNode.Attributes["date"].Value);
                                if (date.Date >= DateTime.Now.Date)
                                {
                                    WeatherItem weatherItem = 
                                        new WeatherItem
                                        {
                                            Temperature = Convert.ToInt32(xmlNode.Attributes["high"].Value),
                                            Date = date,
                                            Text = xmlNode.Attributes["text"].Value,
                                            Code = GetCode(xmlNode),
                                            IsInitialized = true
                                        };
                                    _forecast.Add(weatherItem);
                                }
                            }
                        }
                    }
                }
                IsInitialized = true;
                Logger.Info(string.Format("Weather date {0}", dateString));
            }
            //TODO:
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

        private static int GetCode(XmlNode xmlNode)
        {
            int code;
            if (!int.TryParse(xmlNode.Attributes["code"].Value, out code))
            {
                //TODO: 3200 is Yahoo code Not Available, make it constant
                code = 3200;
            }
            return code;
        }
    }
}
